<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/15
 * Time: 15:30
 */
namespace app\api\controller;

use app\common\controller\Api;

class Test  extends Api {
	protected $noNeedRight = '*';
	protected $noNeedLogin = '*';
	
	public function index(){
		vendor('alipay.AopSdk');

		// 获取支付金额
		$amount='';
		if($_SERVER['REQUEST_METHOD']=='POST'){
			$amount=$_POST['total'];
		}else{
			$amount=$_GET['total'];
		}
		
		$total = floatval($amount);
		if(!$total){
			$total = 1;
		}
		
		$aop = new \AopClient();
		$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
		$aop->appId = "2017122001017740";
		$aop->rsaPrivateKey = 'MIIEpQIBAAKCAQEAzX6piNwJGv6ss5OxmNubnEW2qJA+5Z063eM4J0qVfXNxaIjps59YS3a6l1xDS/EStP8+RHK7e7MXDkD/+SCgSGEs2fkcmos/a0J5ujoTM16hqDhaQHG1qC22c/MEzY9th9sLPXMWSzuxaEx2dBufE2rC833Qv9R7iNmmES8xEwofWLf9v8Jxnpywo1B/1U8EeGtuLP8tZFh79N2tZ6juBmlh+cg35Rm05VrX5DqlXIgW6b3gVqBWlP/fTbnrAczXvXOcROZPVPmK8551ht3MYiKC/ksoi6qRh7YQNaqar2HfCLCAUhvRgoKONzwcVnad2Hr+z8AvzmJq8HGVt5ocIQIDAQABAoIBAQCaceAppHaTe2WBcjfAkw1ZXmvyNLeRC6L6sZO6tCu2pdVOo9hB0DkkPo/uwsExinZs4omCGFkgl3F0nRXFv+e3fpGfIcOFw7Jegw/KD2vu5+jH7dKnLM9DH1hJvRSXCn1f5hmEjcLruGmV4iIFEd11UoBDUQRbqO9P44UU1Usk6tWba6++d2PoIZl8VLjVZh2QBN7Ev/06tXFUNwAeBZBtY9HQf+T3ZCIM+BHXSXku97Tdiz1vtYGjARUd+8PRJoXtqoXFMvAoklExPuymRPdL4Cr09VPHAMhc7hoy/PkLuyiLnw1UETV9zTNrXJA/O3py8vPj/raw0UoMaLGLcEgBAoGBAO+UwIWV5vdsCRqSa2gtty2aItoQLqObbVLi7lYgQmrX7jZcV9ydI6Zv11V2s6XgwdyZ2YL5LQ35141+HF/2u51CjEA4JyahYv09q4mVbXihEzl8mC/m7Iy3Daravm79T8Nky54alLZGWfqXXk4DPR7Eo3dPOHn5khKMgdgLcYnxAoGBANuT5R3ijeu1uol6ePYBqHSWAdFkIOZIjSFJRkJsAHHelh4UWFZWQi34TysIZAchtdNrSrdIz99TgNcIssfORGAA/iB4DsEcWoE39wHeowY0nBQicNt5b2WUoErny6KbY3SKsxFEjKt6A3tsOc0ENKoen9HkTjFn1hF4FoC8/QUxAoGBANDAvD+preZIrGYKzqunqcSCWXvIPdswwsFGGoXQ9g5Ct72GGMiR405ShT5if4Y5oLGnHG+Szj8vQzJ2gCLdyfvs0GfkhbiTRr2BzsXC/e/t6eJdPf3/KH4zwiTtFKGA9s86NHE+l/V6t0wyOGmhSNBWZli/Hmapny2qYDjvP68BAoGBAJFI+ylZ9POIK+O5IH4NvMtfUdadPNOCH7dth0p9sZGmaIeAiK6fpmxKRv+/Log1etxnumkpiTlIRBCD1d80kLNBFOSg64pE23FyeUJIcG+zsa30192wLM/2llTZqDhwPk8nUrJTPAxd0B4DDv+jD8GcP70eJcq03MR0Q9dstLGRAoGADPthZFa5M8gJTXxzjMnLnrVq3srW7iS/q4HPUNljey+9lq+Ph2J4I+Bme7QzjK9xb4AVIOB4i1kJ2N6mOs8wkfnfMsYEJpti23XrCCxmKkmPPlc3JO/FwmjHD8MF54AW5Upc/G/BNYdaNotEHm3HBc9/+B6tIlLk9BUeU+WqF7g=';
		$aop->format = "json";
		$aop->charset = "UTF-8";
		$aop->signType = "RSA2";
		$aop->alipayrsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhizp7j3rKr22vhCrEvfaRq8jkKNyzb8b0tA6y2gz7vL8OG9o/svdUreLKP2WPJiyLog0fFzvztiT3PT3NRO9uYEvmxM5vBzPHSzeLI/r25BP+EQLLj4M5pMW7gR7zwvtv2kFlPkcbPrUlrUk4PC41wrLWFQOMVjgo3cxBVNsqwG43yKXKx2MdJr//HcTEdiUJYj6LvmMTIODoo7OPOEuOR6F2bKzATjk4XD5+1JCOmvuf3jKDbqTRvPggdcbgAbklZZ7nQu1VeSiyBL/DImx0uHwuaDQ5y0X3CmCkZU0O7EQEYb4jQtgcjJ7eRLPUWeZ1XtWzXUwgRi3BIMm/ZP/sQIDAQAB';
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
		$request = new \AlipayTradeAppPayRequest();

// 异步通知地址
		$notify_url = urlencode('https://yl.yunxin123.com/api/order/notifyAliPay');
// 订单标题
		$subject = 'DCloud项目捐赠';
// 订单详情
		$body = 'DCloud致力于打造HTML5最好的移动开发工具，包括终端的Runtime、云端的服务和IDE，同时提供各项配套的开发者服务。';
// 订单号，示例代码使用时间值作为唯一的订单ID号
		$out_trade_no = date('YmdHis', time());

//SDK已经封装掉了公共参数，这里只需要传入业务参数
		$bizcontent = "{\"body\":\"".$body."\","
			. "\"subject\": \"".$subject."\","
			. "\"out_trade_no\": \"".$out_trade_no."\","
			. "\"timeout_express\": \"30m\","
			. "\"total_amount\": \"".$total."\","
			. "\"product_code\":\"QUICK_MSECURITY_PAY\""
			. "}";
		$request->setNotifyUrl($notify_url);
		$request->setBizContent($bizcontent);
//这里和普通的接口调用不同，使用的是sdkExecute
		$response = $aop->sdkExecute($request);

// 注意：这里不需要使用htmlspecialchars进行转义，直接返回即可
		die($response);
	}

// 对签名字符串转义
function createLinkstring($para) {
    $arg  = "";
    while (list ($key, $val) = each ($para)) {
        $arg.=$key.'="'.$val.'"&';
    }
    //去掉最后一个&字符
    $arg = substr($arg,0,count($arg)-2);
    //如果存在转义字符，那么去掉转义
    if(get_magic_quotes_gpc()){$arg = stripslashes($arg);}
    return $arg;
}
// 签名生成订单信息
function rsaSign($data) {
    $priKey = "-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQE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g5Ct72GGMiR405ShT5if4Y5oLGnHG+Szj8vQzJ2gCLdyfvs0GfkhbiTRr2BzsXC/e/t6eJdPf3/KH4zwiTtFKGA9s86NHE+l/V6t0wyOGmhSNBWZli/Hmapny2qYDjvP68BAoGBAJFI+ylZ9POIK+O5IH4NvMtfUdadPNOCH7dth0p9sZGmaIeAiK6fpmxKRv+/Log1etxnumkpiTlIRBCD1d80kLNBFOSg64pE23FyeUJIcG+zsa30192wLM/2llTZqDhwPk8nUrJTPAxd0B4DDv+jD8GcP70eJcq03MR0Q9dstLGRAoGADPthZFa5M8gJTXxzjMnLnrVq3srW7iS/q4HPUNljey+9lq+Ph2J4I+Bme7QzjK9xb4AVIOB4i1kJ2N6mOs8wkfnfMsYEJpti23XrCCxmKkmPPlc3JO/FwmjHD8MF54AW5Upc/G/BNYdaNotEHm3HBc9/+B6tIlLk9BUeU+WqF7g=
-----END RSA PRIVATE KEY-----";
    $res = openssl_get_privatekey($priKey);
    openssl_sign($data, $sign, $res);
    openssl_free_key($res);
    $sign = base64_encode($sign);
    $sign = urlencode($sign);
    return $sign;
}


public function wxPay(){
	//微信支付
	$config = [
		'appid' => 'wx06a1f489a5831340',
		'mch_id' => '1496376012',
		'mch_key' => '568e10999a791ee744d798db108aac44',
	];
	$batchNo = time();
	$totalAmount = 0.1;
	$wxPay = load_wec("Pay",$config);
	$callback = $this->request->domain() . url('api/order/notifyWeChatApp','','');
	$data = [
		"body"              => 'Pay',
		"out_trade_no"      => (string)$batchNo,
		'attach'            => 'payorder', //附加数据(非必填)
		"total_fee"         => $totalAmount*100,
		"notify_url"        => $callback,
		"trade_type"        => 'APP',
		"fee_type"          => 'CNY',
		"spbill_create_ip"  => $this->request->ip()
	];
	
	$res = $wxPay->createOrder($data);
	$prepay_id = $res['prepay_id'];
	$result = $wxPay->createMchPay($prepay_id);
	die($result);
}
}